package com.suzus.medicineGraph.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suzus.medicineGraph.dao.MedicineRepository;
import com.suzus.medicineGraph.dao.SymptomRepository;
import com.suzus.medicineGraph.model.Medicine;
import com.suzus.medicineGraph.model.dto.MedicineDTO;
import com.suzus.medicineGraph.model.Symptom;
import com.suzus.medicineGraph.model.dto.SymptomDTO;
import com.suzus.medicineGraph.service.Neo4jService;
import org.neo4j.driver.internal.value.PathValue;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Service
public class Neo4jServiceImpl implements Neo4jService {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private MedicineRepository medicineRepository;

    @Autowired
    private SymptomRepository symptomRepository;

    @Override
    public HashMap<String, List> findAllRelationShips() {
        List<Medicine> medicineRepositoryAll = medicineRepository.findAll();
        List<MedicineDTO> medicineDTOS = medicineRepositoryAll.stream().map(
                medicine -> {
                    MedicineDTO medicineDTO = new MedicineDTO();
                    BeanUtils.copyProperties(medicine, medicineDTO);
                    medicineDTO.setLabel("Medicine");
                    return medicineDTO;
                }).toList();

        List<Symptom> symptomList = symptomRepository.findAll();
        List<SymptomDTO> symptomDTOS = symptomList.stream().map(
                symptom -> {
                    SymptomDTO symptomDTO = new SymptomDTO();
                    BeanUtils.copyProperties(symptom, symptomDTO);
                    symptomDTO.setLabel("Symptom");
                    return symptomDTO;
                }).toList();


        ArrayList<PathValue> pathValues = symptomRepository.queryRelation(null);
//        System.out.println(pathValues);
        ArrayList<HashMap<String,String>> pathResult = new ArrayList<>();
        for (PathValue pathValue : pathValues) {
            if(pathValue.asPath().start().hasLabel("Medicine")) {
                HashMap<String,String>path=new HashMap<>();
                path.put("source", String.valueOf(pathValue.asPath().start().id()));
                path.put("target", String.valueOf(pathValue.asPath().end().id()));
                path.put("type","治疗");
                pathResult.add(path);
            }
        }

        List<Object> nodeResult = Stream.concat(medicineDTOS.stream(), symptomDTOS.stream()).toList();

        HashMap<String,List> resultMap = new HashMap<>();
        resultMap.put("node",nodeResult);
        resultMap.put("path",pathResult);

        return resultMap;

    }

    @Override
    public HashMap<String, ArrayList> findMedicineRelationShips(String medicine) {

        ArrayList<HashMap<String,String>> pathResult = new ArrayList<>();
        ArrayList<Object>nodeList = new ArrayList<>();
        HashMap<String,ArrayList> resultMap = new HashMap<>();

        Medicine queryMedicine = medicineRepository.queryMedicine(medicine);

        MedicineDTO medicineDTO = new MedicineDTO();
        try {
            BeanUtils.copyProperties(queryMedicine,medicineDTO);
        } catch (IllegalArgumentException e) {
            HashMap<String,String>path=new HashMap<>();
            path.put("target", "1");
            path.put("source", "2");
            path.put("type","None");
            pathResult.add(path);

            nodeList.add(new MedicineDTO(1L,"暂未查到数据","数据库中没有相关信息, 建议使用输入框直接查询药品名称, 不要带“胶囊”“药片”等等","None"));

            resultMap.put("node",nodeList);
            resultMap.put("path",pathResult);

            return resultMap;
        }
        medicineDTO.setLabel("Medicine");

        ArrayList<PathValue> pathValues = medicineRepository.queryMedicineRelation(medicine);

        for (PathValue pathValue : pathValues) {
            HashMap<String,String>path=new HashMap<>();

            Long endId = pathValue.asPath().end().id();

            path.put("source", String.valueOf(pathValue.asPath().start().id()));
            path.put("target", String.valueOf(endId));
            path.put("type","治疗");
            pathResult.add(path);

            Optional<Symptom> querySymptom = symptomRepository.findById(endId);
            System.out.println(querySymptom);
            if (querySymptom.isPresent()) {
                Symptom symptom = querySymptom.get();   //这一段是为了加上给前端用的label标签
                SymptomDTO symptomDTO = new SymptomDTO();
                BeanUtils.copyProperties(symptom,symptomDTO);
                symptomDTO.setLabel("Symptom");
                nodeList.add(symptomDTO);
            }
        }

        nodeList.add(medicineDTO);

        resultMap.put("node",nodeList);
        resultMap.put("path",pathResult);

        return resultMap;

    }

    @Override
    public HashMap<String, ArrayList> findSymptomRelationShips(String symptom) {

        ArrayList<HashMap<String,String>> pathResult = new ArrayList<>();
        ArrayList<Object>nodeList = new ArrayList<>();
        HashMap<String,ArrayList> resultMap = new HashMap<>();

        Symptom queriedSymptom = symptomRepository.querySymptom(symptom);
        SymptomDTO symptomDTO = new SymptomDTO();
        try {
            BeanUtils.copyProperties(queriedSymptom,symptomDTO);
        } catch (IllegalArgumentException e) {
            HashMap<String,String>path=new HashMap<>();
            path.put("target", "1");
            path.put("source", "2");
            path.put("type","None");
            pathResult.add(path);

            nodeList.add(new MedicineDTO(1L,"暂未查到数据","数据库中没有相关信息","None"));

            resultMap.put("node",nodeList);
            resultMap.put("path",pathResult);

            return resultMap;
        }
        symptomDTO.setLabel("Symptom");

        ArrayList<PathValue> pathValues = symptomRepository.querySymptomRelation(symptom);

        for (PathValue pathValue : pathValues) {
            HashMap<String,String>path=new HashMap<>();

            Long endId = pathValue.asPath().start().id();

            path.put("target", String.valueOf(pathValue.asPath().end().id()));
            path.put("source", String.valueOf(endId));
            path.put("type","治疗");
            pathResult.add(path);

            Optional<Medicine> queryMedicine = medicineRepository.findById(endId);
            System.out.println(queryMedicine);
            if (queryMedicine.isPresent()) {
                Medicine medicine = queryMedicine.get();   //这一段是为了加上给前端用的label标签
                MedicineDTO medicineDTO = new MedicineDTO();
                BeanUtils.copyProperties(medicine,medicineDTO);
                medicineDTO.setLabel("Medicine");
                nodeList.add(medicineDTO);
            }
        }

        nodeList.add(symptomDTO);

        resultMap.put("node",nodeList);
        resultMap.put("path",pathResult);

        return resultMap;
    }
}
